package test04;

import java.util.Timer;
import java.util.TimerTask;

enum State {
    SLEEP,
    BOILING,
    MAINTENANCE
}

class WaterHeater {
    private State currentState;
    private int boilingTime;

    public WaterHeater() {
        this.currentState = State.SLEEP;
        this.boilingTime = 0;
    }

    public void transitionStates() {
        Timer timer = new Timer();

        // Simulate the transition at 11 PM
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (currentState == State.SLEEP) {
                    System.out.println("11 PM: Transitioning to BOILING state");
                    currentState = State.BOILING;
                    boilingTime = 0; // Reset boiling time

                    // Schedule the next transition based on boiling time
                    scheduleBoilingTimeCheck(20 * 60 * 1000); // 20 minutes in milliseconds
                }
            }
        }, calculateTimeToElevenPM());

        // Schedule the reset to SLEEP state at 7 AM
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("7 AM: Resetting to SLEEP state");
                currentState = State.SLEEP;
                boilingTime = 0;
            }
        }, calculateTimeToSevenAM());
    }

    private void scheduleBoilingTimeCheck(long delay) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (currentState == State.BOILING) {
                    boilingTime += 20 * 60 * 1000; // Increment boiling time by 20 minutes (for illustration)
                    if (boilingTime >= 20 * 60 * 1000) { // Check if boiling time exceeds 20 minutes
                        System.out.println("Boiling time exceeded 20 minutes: Transitioning to MAINTENANCE state");
                        currentState = State.MAINTENANCE;
                    } else {
                        // In a real implementation, you might want to continue checking periodically
                        // For simplicity, we'll just transition immediately here
                        System.out.println("Boiling for 20 minutes: Transitioning to MAINTENANCE state");
                        currentState = State.MAINTENANCE;
                    }
                    // Note: In a real application, you might want to reset the boiling time or handle it differently
                }
            }
        }, delay);
    }

    private long calculateTimeToElevenPM() {
        // This is a placeholder method. In a real application, you would calculate the actual time difference.
        return 0; // For illustration, assuming it's already 11 PM
    }

    private long calculateTimeToSevenAM() {
        // This is a placeholder method. In a real application, you would calculate the actual time difference.
        return 28800000; // 8 hours in milliseconds (for illustration)
    }

    public static void main(String[] args) {
        WaterHeater heater = new WaterHeater();
        heater.transitionStates();
    }
}